package com.mdd.front.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Product;
import com.mdd.common.entity.ProductCategory;
import com.mdd.common.mapper.ProductCategoryMapper;
import com.mdd.common.mapper.ProductMapper;
import com.mdd.common.util.UrlUtils;
import com.mdd.front.service.IProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements IProductService {

    @Resource
    ProductMapper productMapper;

    @Resource
    ProductCategoryMapper categoryMapper;

    @Override
    public PageResult<Map<String, Object>> list(Map<String, String> params) {
        Integer pageNo = Integer.parseInt(params.getOrDefault("pageNo", "1"));
        Integer pageSize = Integer.parseInt(params.getOrDefault("pageSize", "12"));
        String keyword = params.getOrDefault("keyword", "");
        String categoryId = params.getOrDefault("categoryId", "");
        String sort = params.getOrDefault("sort", "newest");

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.and(wrapper -> wrapper
                    .like("name", keyword)
                    .or()
                    .like("keywords", keyword)
                    .or()
                    .like("description", keyword));
        }

        if (StrUtil.isNotEmpty(categoryId) && !categoryId.equals("0")) {
            queryWrapper.eq("category_id", categoryId);
        }

        if (sort.equals("newest")) {
            queryWrapper.orderByDesc("is_new", "create_time");
        } else if (sort.equals("popular")) {
            queryWrapper.orderByDesc("is_hot", "views");
        }

        IPage<Product> iPage = productMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);

        List<Map<String, Object>> list = iPage.getRecords().stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", item.getId());
            map.put("name", item.getName());
            map.put("image", UrlUtils.toAbsoluteUrl(item.getImage()));
            map.put("description", item.getDescription());
            map.put("categoryId", categoryMapper.selectById(item.getCategoryId()).getName());
            map.put("isHot", item.getIsHot());
            map.put("isNew", item.getIsNew());
            return map;
        }).collect(Collectors.toList());

        PageResult<Map<String, Object>> pageResult = new PageResult<>();
        pageResult.setCount(iPage.getTotal());
        pageResult.setLists(list);
        return pageResult;
    }

    @Override
    public Map<String, Object> detail(Integer id) {
        Product product = productMapper.selectById(id);
        ProductCategory category = categoryMapper.selectById(product.getCategoryId());
        if (product == null) {
            return null;
        }

        product.setViews(product.getViews() + 1);
        productMapper.updateById(product);

        Map<String, Object> map = new HashMap<>();
        map.put("id", product.getId());
        map.put("name", product.getName());
        map.put("image", UrlUtils.toAbsoluteUrl(product.getImage()));
        map.put("images", product.getImages());
        map.put("description", product.getDescription());
        map.put("details", product.getDetails());
        map.put("categoryId", product.getCategoryId());
        map.put("isHot", product.getIsHot());
        map.put("isNew", product.getIsNew());
        map.put("views", product.getViews());
        map.put("categoryName",category.getName());
        return map;
    }

    @Override
    public Map<String, Object> category() {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("sort");
        
        List<ProductCategory> categories = categoryMapper.selectList(queryWrapper);
        List<Map<String, Object>> list = categories.stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", item.getId());
            map.put("name", item.getName());
            map.put("icon", item.getIcon());
            return map;
        }).collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        return result;
    }
}